//
//  UIButton+Block.m
//  SharedGym
//
//  Created by Jiang on 2017/4/19.
//
//

#import "UIControl+Block.h"
#import <objc/runtime.h>

static NSString *const JMBlockCacheKey = @"blockCacheKey";

@implementation UIControl (ResponseWithBlock)

+ (instancetype )controlWithBlock:(JIMControlCallBack)block{
    return [[self class] controlWithBlock:block forControlEvents:UIControlEventTouchUpInside];
}

+ (instancetype)controlWithBlock:(JIMControlCallBack)block forControlEvents:(UIControlEvents)event{
    UIButton *button = [[[self class]alloc]init];
    [button addActionForEvent:event block:block];
    return button;
}
void customButtonBlockPerform(UIButton *caller,SEL sel){
    JIMControlCallBack block = [caller JIMBlockCache][NSStringFromSelector(sel)];
    block(caller);
}

- (void)addActionForEvent:(UIControlEvents)event block:(JIMControlCallBack)block{
    // 为event 的block注册selector
    NSString *selecorName  = [NSString stringWithFormat:@"CustomButtonBlock_%ld",event];
    SEL sel = NSSelectorFromString(selecorName);
    if (!class_getClassMethod(self.class, sel)) {
        class_addMethod([self class], sel, (void(*)(void))&customButtonBlockPerform, "v@:");
    }
    //保存block
    [self JIMBlockCache][selecorName] = block;
    [self addTarget:self action:sel forControlEvents:event];
}
- (void)addUpInsideAction:(JIMControlCallBack)block{
    [self addActionForEvent:UIControlEventTouchUpInside block:block];
}

#pragma mark - GET & SET
- (NSMutableDictionary *)JIMBlockCache{
    NSMutableDictionary *blockCaches =  objc_getAssociatedObject(self, &JMBlockCacheKey);
    if (blockCaches == nil) {
        blockCaches = [NSMutableDictionary dictionary];
        [self willChangeValueForKey:JMBlockCacheKey];
        objc_setAssociatedObject(self, &JMBlockCacheKey, blockCaches , OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        [self didChangeValueForKey:JMBlockCacheKey];
    }
    return blockCaches;
}



@end
static char kUIButtonBadgeViewKey = '\0';
static char kUIButtonBadgeLabelKey = '\0';
@implementation UIButton (BadgeValue)
- (void)setBadgeView:(UIView *)badgeView{
    objc_setAssociatedObject(self, &kUIButtonBadgeViewKey, badgeView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIView *)badgeView{
    UIView *view = objc_getAssociatedObject(self, &kUIButtonBadgeViewKey);
    if (!view) {
        view = [UIView new];
        view.backgroundColor = [UIColor redColor];
        self.badgeView = view;
    }
    return view;
}
- (void)setBadgeLabel:(UILabel *)badgeLabel{
    objc_setAssociatedObject(self, &kUIButtonBadgeLabelKey, badgeLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UILabel *)badgeLabel{
    UILabel *label = objc_getAssociatedObject(self, &kUIButtonBadgeLabelKey);
    if (!label) {
        label = [UILabel new];
        label.font = [UIFont systemFontOfSize:12];
        label.textColor = [UIColor whiteColor];
        self.badgeLabel = label;
    }
    return label;
}

- (void)setBadgeValue:(NSString *)badgeValue{
    self.badgeView.hidden = badgeValue == nil;
    if (!badgeValue) {
        return;
    }
    if (!self.badgeView.superview) {
        [self.badgeView addSubview:self.badgeLabel];
        [self addSubview:self.badgeView];
        self.badgeView.translatesAutoresizingMaskIntoConstraints = NO;
        self.badgeLabel.translatesAutoresizingMaskIntoConstraints = NO;
        [NSLayoutConstraint activateConstraints:@[
                                                    [self.badgeView.topAnchor constraintEqualToAnchor:self.topAnchor],
                                                    [self.badgeView.rightAnchor constraintEqualToAnchor:self.rightAnchor],
                                                    [self.badgeView.widthAnchor constraintEqualToAnchor:self.badgeView.heightAnchor multiplier:1]
                                                  ]];
        
        [NSLayoutConstraint activateConstraints:@[
                                                  [self.badgeLabel.leftAnchor constraintEqualToAnchor:self.badgeView.leftAnchor constant:8],
                                                  [self.badgeLabel.rightAnchor constraintEqualToAnchor:self.badgeView.rightAnchor constant:-8],
                                                  [self.badgeLabel.centerYAnchor constraintEqualToAnchor:self.badgeView.centerYAnchor]
                                                  ]];
        
        
    }
    self.badgeLabel.text = badgeValue;
    [self.badgeView layoutIfNeeded];
    self.badgeView.layer.cornerRadius = self.badgeView.frame.size.width * 0.5;
    self.badgeView.layer.masksToBounds = YES;
}

- (NSString *)badgeValue{
    return self.badgeLabel.text;
}
- (void)flipContent{
    [self layoutIfNeeded];
    CGFloat imageWidth = self.imageView.frame.size.width;
    CGFloat titleWidth = self.titleLabel.frame.size.width;
    CGFloat width = self.frame.size.width;
    CGFloat offset = width - imageWidth - titleWidth;
    offset = offset > 0 ? offset/2 : 0;
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -(imageWidth + offset), 0, imageWidth + offset)];
    [self setImageEdgeInsets:UIEdgeInsetsMake(0, titleWidth + offset, 0, -(titleWidth + offset))];
}
@end

